package com.tdh.dao.impl;

import com.tdh.dao.UserDao;
import com.tdh.pojo.TUser;
import com.tdh.utils.StringUtil;
import org.hibernate.*;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    //使用Hibernate提供的模板
    @Resource
    private HibernateTemplate hibernateTemplate;

    @Resource
    private SessionFactory sessionFactory;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public List<TUser> selectByIdAndPwd(String yhid, String yhkl) {

        TUser user = new TUser();
        user.setYhid(yhid);
        user.setYhkl(yhkl);
        return hibernateTemplate.findByExample(user);
    }

    @Override
    public void insertUser(TUser user) {
        hibernateTemplate.save(user);
    }

    @Override
    public void updateUser(TUser user) {
        hibernateTemplate.update(user);
    }

    @Override
    public void deleteUser(String yhid) {

        TUser tUser = new TUser();
        tUser.setYhid(yhid);
        hibernateTemplate.delete(tUser);
    }

    @Override
    public TUser selectUserById(String yhid) {

        Session session = hibernateTemplate.getSessionFactory().openSession();
        TUser user = (TUser) session.get(TUser.class, yhid);
        session.close();
        return user;
    }

    @Override
    public List<TUser> selectUserTableLimit(String yh, String bm, int startRow, int limitNum) {

        Session session = sessionFactory.openSession();

        //创建查询hql字符串
        StringBuilder sb = new StringBuilder("select new TUser(user.yhid, user.yhxm, user.yhkl, user.yhbm, user.yhxb, user.csrq, user.sfjy, user.pxh) from TUser user");
        if(bm != null && !bm.equals("")) {
            sb.append(" and user.yhbm ='").append(bm).append("'");
        }
        if(yh != null && !yh.equals("")) {
            sb.append(" and (user.yhid like '%")
                    .append(yh).append("%' or user.yhxm like '%")
                    .append(yh).append("%')");
        }
        sb.append(" order by user.pxh, user.yhid");
        String hql = sb.toString().replaceFirst(" and ", " where ");

        Query query = session.createQuery(hql);
        query.setFirstResult(startRow);
        query.setMaxResults(limitNum);
        List<TUser> list = query.list();

        return list;
    }

    @Override
    public String countRows(String yh, String bm) {

        Session session = sessionFactory.openSession();

        //创建查询hql字符串
        StringBuilder sb = new StringBuilder("select count(user) from TUser user");
        if(bm != null && !bm.equals("")) {
            sb.append(" and user.yhbm ='").append(bm).append("'");
        }
        if(yh != null && !yh.equals("")) {
            sb.append(" and (user.yhid like '%")
                    .append(yh).append("%' or user.yhxm like '%")
                    .append(yh).append("%')");
        }
        String hql = sb.toString().replaceFirst(" and ", " where ");
        Query query = session.createQuery(hql);
        List list = query.list();
        return list.get(0).toString();
    }

    @Override
    public int queryPxh() {
        Session session = sessionFactory.openSession();
        String hql = "select MAX(pxh) from TUser";
        Query query = session.createQuery(hql);
        List list = query.list();
        System.out.println(list.get(0));
        return (Integer)list.get(0);
    }

}
